<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="账户"
          label="账户"
          placeholder="账户"
          :rules="[{ required: true, message: '请填写账户' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">

import myAxios from "../plugin/myAxios.js";
import {showFailToast, showSuccessToast} from "vant";
import {ref} from "vue";
import {useRouter} from "vue-router";

const router = useRouter()

const userAccount = ref('');
const userPassword = ref('');
const onSubmit = async (values: any) => {
  console.log('submit', values);

  const loginUser = await myAxios.post("/user/login", {
    "userAccount": userAccount.value,
    "userPassword": userPassword.value
  }).then(resp => {
    return resp
  })

  if (loginUser) {
    console.log(loginUser)
    showSuccessToast(`欢迎您，${loginUser.data.userName}`)
    //等待显示完后
    await router.replace("/");
  } else {
    showFailToast("登录失败...")
  }

};
</script>

<style scoped>

</style>